//
//  WenTiFuDaoNavView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/17.
//  Copyright © 2019 SanYi. All rights reserved.
//

#import "WenTiFuDaoNavView.h"

@implementation WenTiFuDaoNavView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame andArray:(NSArray *)buttonArray{
    if ([super initWithFrame:frame]) {
        dataArray = buttonArray;
        [self initSubView];
    }
    return self;
}
- (void)initSubView{
    self.backgroundColor = MAINBACKCOLOR;
    _mainw = MAINW;
    lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColorChange colorwithHexString:@"#20B097"];
    [self addSubview:lineView];
    CGFloat W = (_mainw - 300)/3;
    for (int i  =0; i<dataArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(W*i+50, 5,W, 30);
        [button setTitle:dataArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:lineView.backgroundColor forState:UIControlStateSelected];
        [button setTitleColor:TITLECOLOR forState:UIControlStateNormal];
        button.tag = 100+i;
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [self addSubview:button];
        if (i == 0) {
            button.selected = YES;
            lineView.frame = CGRectMake(0, 0, 90, 2);
            lineView.center = CGPointMake(button.center.x, button.center.y+20);
        }
    }
    [self addSubview:self.backButton];
    _backButton.hidden = YES;
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@50);
        make.height.equalTo(@20);
    }];
    [self addSubview:self.timeButton];
    [_timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_right).offset(-230);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@20);
    }];
    [self addSubview:self.nianjiButton];
    [_nianjiButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.timeButton.mas_right).offset(5*fw);
        make.centerY.equalTo(self.mas_centerY);
        make.width.equalTo(@100);
        make.height.equalTo(@20);
    }];
}
- (void)uploadLayout{
    CGFloat W = (_mainw - 300)/3;
    for (int i  =0; i<dataArray.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:i+100];
        button.frame = CGRectMake(W*i+50, 5,W, 30);
        if (button.selected == YES) {
            lineView.frame = CGRectMake(0, 0, 90, 2);
            lineView.center = CGPointMake(button.center.x, button.center.y+20);
        }
    }
}
- (void)buttonClick:(UIButton *)sender{
    for (int i = 0; i<dataArray.count; i++) {
        UIButton *button = (UIButton *)[self viewWithTag:100+i];
        button.selected = NO;
    }
    sender.selected = YES;
    [UIView animateWithDuration:0.5 animations:^{
        self->lineView.center = CGPointMake(sender.center.x, sender.center.y+20);
    }];
    [self.delegate scrollViewChangeButtom:(int)sender.tag-100];
    
}
- (UIButton *)backButton{
    if (!_backButton) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:@" 返回" forState:UIControlStateNormal];
        [button setTitleColor:[UIColorChange colorwithHexString:@"SELECTEDCOLOR"] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:12];
        [button setTitleColor:SELECTEDCOLOR forState:0];
        [button addTarget:self action:@selector(back:) forControlEvents:UIControlEventTouchUpInside];
        _backButton = button;
    }
    return _backButton;
}
- (void)back:(UIButton *)sender{
    [self.delegate back];
}
- (MinButtonViewBtn *)timeButton{
    if (!_timeButton) {
        MinButtonViewBtn *btn = [[MinButtonViewBtn alloc]initWithFrame:CGRectMake(550*fw, 15, 100, 20) andType:ButtonImageInRight];
        btn.bottomLabel.text = [PublickFunctionObject getTimeCurrent];
        btn.bottomLabel.font = [UIFont systemFontOfSize:12];
        btn.bottomLabel.textColor = TITLECOLOR;
        btn.topImage.image = [UIImage imageNamed:@"arrow_down"];
        [btn addTarget:self action:@selector(timeClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn.bottomLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0).offset(-15);
        }];
        _timeButton = btn;
    }
    return _timeButton;
}
- (void)timeClick:(MinButtonViewBtn *)sender{
    [self.delegate riQiSelected];
}
- (MinButtonViewBtn *)nianjiButton{
    if (!_nianjiButton) {
        MinButtonViewBtn *btn = [[MinButtonViewBtn alloc]initWithFrame:CGRectMake(550*fw, 15, 69, 20) andType:ButtonImageInRight];
        btn.bottomLabel.text = @"选择班级";
        btn.bottomLabel.font = [UIFont systemFontOfSize:12];
        btn.bottomLabel.textColor = TITLECOLOR;
        btn.topImage.image = [UIImage imageNamed:@"arrow_down"];
        [btn addTarget:self action:@selector(nianjiButton:) forControlEvents:UIControlEventTouchUpInside];
        [btn.bottomLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@0);
            make.right.equalTo(@0).offset(-15);
        }];
        _nianjiButton = btn;
    }
    return _nianjiButton;
}
- (void)nianjiButton:(MinButtonViewBtn *)sender{
    [self.delegate nianJiSelected];
}
@end
